package com.huawei.classroom.student.h60;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerThread extends Thread{
	private Socket socket;
	private BufferedReader in = null;
	private PrintWriter out  = null;
	public ServerThread(Socket socket) {
		super();
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new java.io.PrintWriter(socket.getOutputStream());
			String line = in.readLine();
			while (!"BYE".equals(line)) {//我用BYE来当客户的退出登录
				for(int i=0; i<ChatServer.ThreadList.size(); i++) {//群发消息
					ChatServer.ThreadList.get(i).out.write(line + "\r\n");
					ChatServer.ThreadList.get(i).out.flush();
				}
				line = in.readLine();
				//System.out.println(line);
			}
			in.close();
			out.close();
			socket.close();
		} catch (IOException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
}
